package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.StoreSalesMapper;
import com.qhg.saas_server.pojo.dto.StoreSalesDto;
import com.qhg.saas_server.service.StoreSalesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class StoreSalesServiceImpl implements StoreSalesService {

    @Autowired
    StoreSalesMapper storeSalesMapper;

    /**
     * 条件查询门店销售动态
     * @param storeSalesDto
     * @return
     */
    @Override
    public PageInfo<StoreSalesDto> getStoreSalesByConditon(StoreSalesDto storeSalesDto) {

        String sortRule ="ASC";

        //判断排序方式方式，按照销售额排序
        Integer rule = storeSalesDto.getSortRule();
        if(!Objects.isNull(rule)){
            sortRule = rule == 1 ? "DESC" : "ASC";
        }

        //设置默认的serchRadius 如果没有参数则默认查询本日记录
        Integer serchRadius =storeSalesDto.getSerchRadius();
        if(Objects.isNull(serchRadius)){
            serchRadius = 1;
        }

        //开启分页插件
        PageHelper.startPage(storeSalesDto.getPageNum(),storeSalesDto.getPageSize(),"salesVolume  "+sortRule);

        List<StoreSalesDto> list = storeSalesMapper.getStoreSalesByConditon(storeSalesDto);

        list.stream().forEach((item)->{
            //获取商店信息
            Integer storeId = item.getStoreId();

            //获取该商店的退款金额
         Double  refundVolume = storeSalesMapper.getRefundVolume(storeId,storeSalesDto);

         //将退款金额填充的实体中
            item.setRefundVolume(refundVolume);

        });
        PageInfo<StoreSalesDto> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
